const arr = [1, [2, [3, 4]]];
// console.log(flatDeep(arr, Infinity)); // [1, 2, 3, 4]
// console.log(flatDeep(arr, 0)); // [1, 2, 3, 4]
let arr2 = flatDeep(arr, 0)
arr2.shift()
console.log(arr2); // [1, 2, 3, 4]
console.log(arr); // [1, 2, 3, 4]

function flatDeep(arr, d = 1) {
    return d > 0 ?
        arr.reduce((acc, val) => acc.concat(Array.isArray(val) ?
            flatDeep(val, --d) : val), []) :
        arr.slice()
}